package com.opo5.logisticsmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opo5.logisticsmanagement.Vo.StatisUserVo;
import com.opo5.logisticsmanagement.Vo.UserVo;
import com.opo5.logisticsmanagement.common.MD5Utils;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.dao.AddressDao;
import com.opo5.logisticsmanagement.dao.DepartmentDao;
import com.opo5.logisticsmanagement.dao.RoleDao;
import com.opo5.logisticsmanagement.dao.UserDao;
import com.opo5.logisticsmanagement.domain.Address;
import com.opo5.logisticsmanagement.domain.Department;
import com.opo5.logisticsmanagement.domain.Role;
import com.opo5.logisticsmanagement.domain.User;
import com.opo5.logisticsmanagement.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author lixin
* @description 针对表【tbs_user】的数据库操作Service实现
* @createDate 2022-07-10 08:55:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User>
    implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public R getUserByCondition(String nickname, Integer currentPage, Integer pageSize) {
        Page<User> page = new Page<>(currentPage,pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(nickname!=null,"nickname",nickname);
        userDao.selectPage(page,queryWrapper);
        List<User> users = page.getRecords();
        List<UserVo> userVos = new ArrayList<>();
        for (User user : users){
            UserVo userVo = new UserVo();
            Address address = addressDao.selectById(user.getAddressId());
            Department dept = departmentDao.selectById(user.getDeptId());
            Role role = roleDao.selectById(user.getRoleId());
            userVo.setUser(user);
            userVo.setAddress(address);
            userVo.setDepartment(dept);
            userVo.setRole(role);
            userVos.add(userVo);
        };
        return R.success(new R().add("users",userVos).add("total",page.getTotal()).getMap());
    }

    @Override
    public R getUserById(Integer id) {
        User user = userDao.selectById(id);
        Address address = addressDao.selectById(user.getAddressId());
        Department dept = departmentDao.selectById(user.getDeptId());
        Role role = roleDao.selectById(user.getRoleId());
        UserVo userVo = new UserVo();
        userVo.setUser(user);
        userVo.setAddress(address);
        userVo.setRole(role);
        userVo.setDepartment(dept);
        return R.success(new R().add("userInfo",userVo).getMap());
    }

    @Override
    @Transactional
    public R addUser(UserVo userVo) {
        Address address = userVo.getAddress();
        User user = userVo.getUser();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(user.getNickname()!=null,"nickname",user.getNickname());
        Integer count = userDao.selectCount(queryWrapper);

        if (count!=0){
            return R.error("用户名已经存在");
        }

        addressDao.insert(address);
        user.setPassword(MD5Utils.encrypt(user.getPassword(),MD5Utils.MD5_KEY));
        user.setAddressId(address.getId());
        int flag = userDao.insert(user);
        if (flag==1){
            return R.success(null);
        }
        return R.error("添加员工信息失败");
    }

    @Override
    @Transactional
    public R editUser(UserVo userVo) {
        Address address = userVo.getAddress();
        User user = userVo.getUser();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(user.getNickname()!=null,"nickname",user.getNickname());
        User user1 = userDao.selectOne(queryWrapper);

        if (user1 != null && !(user1.getId().equals(user.getId()))){
            return R.error("用户名已经存在");
        }

        addressDao.updateById(address);

        if (user.getPassword()==null) {
            user.setPassword(user1.getPassword());
        }else{
            user.setPassword(MD5Utils.encrypt(user.getPassword(),MD5Utils.MD5_KEY));
        }

        int flag = userDao.updateById(user);

        if (flag==1){
            return R.success(null);
        }

        return R.error("修改员工信息失败");
    }

    @Override
    public R delUser(Integer[] ids) {
        int i = userDao.deleteBatchIds(Arrays.asList(ids));
        if(i != ids.length){
            return R.error("删除了"+i+"条,"+"失败"+(ids.length-i)+"条");
        }
        return R.success(null);
    }

    @Override
    public Page<StatisUserVo> statisUser(Page ipage, int year) {
        return userDao.user(ipage);
    }
//    public Page<MaterialVo> getPageVo(Page<MaterialVo> iPage) {
//        return planListDao.getElectricity(iPage);
//    }

}




